import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;

public class MyCircularQueue {

    private int[] elem;
    private int front;
    private int rear;

    public MyCircularQueue(int k) {
        //浪费一个空间来实现判断它是否已满
        this.elem = new int[k+1];
    }


    //向循环队列插入一个元素。如果成功插入则返回真。
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        elem[rear] = value;
        rear = (rear+1) % elem.length;
        return true;
    }


    //从循环队列中删除一个元素。如果成功删除则返回真。
    public boolean deQueue() {
        if (isEmpty()){
            return false;
        }
        int ret = elem[front];
        front = (front+1) % elem.length;
        return true;
    }

    //从队首获取元素。如果队列为空，返回 -1
    public int Front() {
        if (isEmpty()){
            return -1;
        }
        return elem[front];
    }


    // 获取队尾元素。如果队列为空，返回 -1
    public int Rear() {
        if (isEmpty()){
            return -1;
        }
        int index = (rear == 0) ? (elem.length-1) : rear-1;
        return elem[index];
    }

    public boolean isEmpty() {
     return front == rear;
    }

    public boolean isFull() {
        if((rear+1) % elem.length == front){
            return true;
        }
        return false;
    }
}
